﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace hw3_1
{
    internal class Program
    {
        static string DeleteComments(string line)
        {
            int index = line.IndexOf("//");  // 查找注释起始位置

            if (index != -1)
            {
                return line.Substring(0, index);  // 返回注释前的部分
            }
            else
            {
                return line;  // 没有注释直接返回原字符串
            }
        }

        static int getWordNum(string s)
        {
            int i = 0;
            char c;
            int p = 0; // 记录上一次
            int q = 0; // 记录当前
            int wordNum = 0;
            while (i <= s.Length - 1)
            {
                c = s[i];
                if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122)) // 字母
                    q = 1;
                else
                    q = 0;
                if (p == 0 && q == 1)
                    wordNum++;
                p = q;
                i++;
            }
            return wordNum;
        }

        static void Main(string[] args)
        {
            try
            {
                FileStream fin = new FileStream("../../CSHarpFile.cs", FileMode.Open, FileAccess.Read);
                FileStream fout = new FileStream("../../new.txt", FileMode.Create, FileAccess.Write);
                using (StreamReader reader = new StreamReader(fin, Encoding.Default))
                {
                    using (StreamWriter writer = new StreamWriter(fout, Encoding.Default))
                    {
                        int lineNum = 0;
                        int lineNum2 = 0;
                        int wordNum = 0;
                        int wordNum2 = 0;
                        string s = null;

                        Dictionary<string, int> wordCount = new Dictionary<string, int>();

                        while ((s = reader.ReadLine()) != null)
                        { // 按行读源文件
                            lineNum++;
                            int w = getWordNum(s);
                            wordNum += w;
                            // 去掉空行和注释，并写入新的文件new.txt
                            if (!string.IsNullOrWhiteSpace(s))
                            { // 不是空行
                                s = DeleteComments(s); // 去掉以//开始的注释
                                if (!string.IsNullOrWhiteSpace(s))
                                {
                                    writer.WriteLine(lineNum + "(" + w.ToString() + "): \t" + s); // 写入到目标文件
                                    lineNum2++;
                                    wordNum2 += getWordNum(s);

                                    string[] words = s.Split(new char[] { ' ', '\t', '\r', '\n', ',', '.', ';', ':', '(', ')', '[', ']', '{', '}', '<', '>', '+', '-', '*', '/', '=', '!', '&', '|', '^', '%', '@','"' });
                                    foreach (string word in words)
                                    {
                                        if (!string.IsNullOrWhiteSpace(word)) // 排除空字符串
                                        {
                                            if (wordCount.ContainsKey(word))
                                            {
                                                wordCount[word]++;
                                            }
                                            else
                                            {
                                                wordCount[word] = 1;
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        Console.WriteLine("CSharpFile.cs的原始行数是" + lineNum.ToString() + "，原始单词数是" + wordNum.ToString());
                        Console.WriteLine("去掉空行和注释后的行数是" + lineNum2.ToString() + "，单词数是" + wordNum2.ToString());

                        Console.WriteLine("每个单词出现的次数：");
                        foreach (var pair in wordCount)
                        {
                            Console.WriteLine(pair.Key + ": " + pair.Value);
                        }
                    }
                }
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine($"File {e.FileName} is not found!");
            }
            catch (IOException e2)
            {
                Console.WriteLine(e2);
            }
            Console.Read();
        }
    }
}